﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package eu.orangeflash.webapis.xmlrpc.utils {
    import eu.orangeflash.webapis.xmlrpc.*;

    public class Deserializer implements IXMLRPCDeserializer {

        public static const NULL:String = "null";
        public static const NUMBER:String = "Number";
        public static const BOOL:String = "Boolean";
        public static const DATE:String = "Date";
        public static const INT:String = "int";
        public static const STRING:String = "String";
        public static const BASE64:String = "base64";
        public static const ARRAY:String = "Array";
        public static const OBJECT:String = "Object";

        public function Deserializer(){
            super();
        }
        protected function createObject(value:XMLList):Object{
            var result:Object;
            var v:XMLList;
            var item:XML;
            result = new Object();
            v = value.struct;
            for each (item in v.member) {
                result[item.name.toString()] = createType(item.value);
            };
            return (result);
        }
        public function createResponse(xml:XML):IMethodResponse{
            var params:Array;
            var fault:Fault;
            var result:IMethodResponse;
            var pars:XMLList;
            var param:XML;
            var fObj:Object;
            if (xml.children().name() == "params"){
                pars = xml.params.param.value;
                params = new Array();
                for each (param in pars) {
                    params.push(createType(XMLList(param)));
                };
            } else {
                fObj = createType(xml.fault.value);
                fault = new Fault(fObj.faultCode, fObj.faultString);
            };
            result = new MethodResponse(params, fault);
            return (result);
        }
        protected function createType(value:XMLList):Object{
            var result:Object;
            var type:String;
            type = getComplexType(value);
            if (type == ""){
                result = createSimple(value);
            } else {
                if (type == OBJECT){
                    result = createObject(value);
                } else {
                    if (type == ARRAY){
                        result = createArray(value);
                    } else {
                        result = createDate(value);
                    };
                };
            };
            return (result);
        }
        protected function createDate(value:XMLList):Date{
            var rawDate:String;
            var tempDate:Array;
            var date:String;
            var time:String;
            var tempTime:Array;
            rawDate = value.children().toString();
            tempDate = rawDate.split("T");
            date = tempDate[0];
            time = tempDate[1];
            tempTime = time.split(":");
            return (new Date(Number(date.slice(0, 4)), (Number(date.slice(4, 6)) - 1), Number(date.slice(6, 8)), Number(tempTime[0]), Number(tempTime[1]), Number(tempTime[2])));
        }
        protected function getComplexType(value:XMLList):String{
            var type:String;
            var result:String;
            type = value.children().name();
            switch (type){
                case "dateTime.iso8601":
                    result = DATE;
                    break;
                case "array":
                    result = ARRAY;
                    break;
                case "struct":
                    result = OBJECT;
                    break;
                default:
                    result = "";
            };
            return (result);
        }
        protected function createArray(value:XMLList):Array{
            var result:Array;
            var v:XMLList;
            var item:XML;
            result = new Array();
            v = value.array.data.value;
            for each (item in v) {
                result.push(createType(XMLList(item)));
            };
            return (result);
        }
        protected function createSimple(value:XMLList):Object{
            var v:XMLList;
            var type:String;
            var result:Object;
            v = value.children();
            type = v.name();
            switch (type){
                case "string":
                    result = v.toString();
                    break;
                case "double":
                    result = Number(v.toString());
                    break;
                case "int":
                    result = int(v.toString());
                    break;
                case "i4":
                    result = int(v.toString());
                    break;
                case "boolean":
                    result = Boolean(v.toString());
                    break;
                case "base64":
                    result = v.toString();
                    break;
            };
            return (result);
        }

    }
}//package eu.orangeflash.webapis.xmlrpc.utils 
